package net.zypro.sample.model;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.VolleyError;

import net.zypro.sample.C;
import net.zypro.sample.net.GsonRequest;
import net.zypro.sample.net.NetErrorListener;
import net.zypro.sample.util.LogUtils;
import net.zypro.sample.util.NetUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by mac on 15/5/13.
 */
public class AccountManager {
    private static final String TAG= LogUtils.makeTag(AccountManager.class);

    public void login(String username,String password, final ILoginListener mListener)
    {
        Map<String,String> params=new HashMap<String, String>();
        params.put("username",username);
        params.put("password",password);

        GsonRequest<Account> req=new GsonRequest<Account>(
                C.Url.LOGIN,
                params,
                Account.class,
                new Response.Listener<Account>() {
                    @Override
                    public void onResponse(Account response) {
                        LogUtils.LogD(TAG,response.toString());

                        if(mListener!=null)
                        {
                            mListener.loginSuccess(response);
                        }
                    }
                },
                new NetErrorListener() {
                    @Override
                    public void onError(ErrorMessage msg) {
                        LogUtils.LogD(TAG,msg.toString());

                        if(mListener!=null)
                        {
                            mListener.loginFailed(msg);
                        }
                    }
                }
        );

        NetUtils.getInstance().addTask(req);
    }

    public interface ILoginListener
    {
        public void loginFailed(ErrorMessage msg);
        public void loginSuccess(Account account);
    }
}
